<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>任务办理</title>
    <!-- axios -->
    <script src="./js/axios.min.js"></script>
    <!-- vue -->
    <script src="./js/vue.js"></script>
</head>

<body>
<div id="root">发送消息中url的指向页面:{{test}}</div>

<script type="text/javascript">

    let interface_ip_port = "http://111.17.201.197:999";
    let page_ip_port = "http://111.17.201.197:55000";
    new Vue({
        el: "#root",
        data: {
            test: "wwq",
            token: ""
        },
        beforeMount() {
            //组建创建之后首先同获取url参数中的办理页面地址
            let handlePageUrl = this.getParams("param");
            let innerHandlerPageUrl = localStorage.getItem("handlePageUrl");
            if (this.isEmpty(innerHandlerPageUrl) && !this.isEmpty(handlePageUrl)
                ||
                !this.isEmpty(innerHandlerPageUrl) && !this.isEmpty(handlePageUrl) && innerHandlerPageUrl != handlePageUrl) {
                //如果innerHandlerPageUrl为空，或者，innerHandlerPageUrl不为空且和handlePageUrl不想等。那么就将handlePageUrl存储到localStorage
                localStorage.setItem("handlePageUrl", JSON.stringify(handlePageUrl));
            }
        },
        mounted() {
            this.token = localStorage.getItem("zhqh-pro-token");
            let handlerPage = localStorage.getItem("handlePageUrl");
            if (!this.isEmpty(this.token) && this.judgeToken(this.token)) {
                if (!this.isEmpty(handlerPage)) {
                    alert(JSON.parse(handlerPage))
                    this.clickA(JSON.parse(handlerPage));
                } else {
                    alert("未找到办理页面");
                    return;
                }
            } else {
                /*
                    token不存或者无效就引导用户授权，跳转到回调页面（ipc备案的域名下），在回调页面中拿到code，
                    后端根据code获取用户openID，通过openID查询用户信息，封装token返回
                */
                this.clickA(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8066c3c5aade3186&redirect_uri=${page_ip_port}/wechat/getToken.html&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`)
            }
        },
        methods: {
            //获取url参数
            getParams(key) {
                //拿到跳转时完整的url
                var href = window.location.href;
                //解析出名为`key`第一个参数
                if (href) {
                    let p = href.split(`${key}=`)
                    return unescape(p[1]);
                }
                return null;
            },
            //检验token是否失效
            async judgeToken(token) {
                return new Promise((resolve) => {
                    axios.get(`${interface_ip_port}` + "/sys/checkToken" + `?token=${token}`).then((res) => {
                        resolve(res.data.result);
                    });
                });
            },
            //创建a标签并模拟点击事件
            clickA(url) {
                if (this.isEmpty(url)) {
                    alert("参数错误");
                    return;
                }
                let a = document.createElement("a");
                a.style.display = "none";
                a.href = url;
                document.body.appendChild(a);
                //点击
                a.click();
                // 下载完成移除元素
                document.body.removeChild(a);
            },
            //判空,是否为空，是空就返回真
            isEmpty(data) {
                if (data != null && data != "" && data != undefined && data != "undefined") {
                    return false;
                }
                return true;
            }
        }
    })
</script>
</body>

</html>